/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Employee.java
 *
 * Created on May 6, 2011, 7:19:52 PM
 */
package com.abc.salesinventory.ui;



import com.abc.salesinventory.model.Item;
import com.abc.salesinventory.model.newpackage.Stock;
import com.abc.salesinventory.service.StockService;
import com.abc.salesinventory.service.StockServiceImpl;
import com.abc.salesinventory.util.DatePicker;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Sasika Suchithra
 */
public class NewStock extends javax.swing.JFrame {

    private StockService stockService = new StockServiceImpl();
    private SearchItemForm searchItemForm = new SearchItemForm();
    private Item selectedItem = null;

    /** Creates new form Employee */
    public NewStock() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jPanel1 = new javax.swing.JPanel();
        unit = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnsave = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        btncls = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        qty = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        expDate = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        supplier = new javax.swing.JLabel();
        brandName = new javax.swing.JLabel();
        category = new javax.swing.JLabel();
        searchItem = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        code = new javax.swing.JLabel();

        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Stock");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);
        setLocationByPlatform(true);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        unit.setText("Unit");
        jPanel1.add(unit, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 200, 50, 20));

        jLabel2.setText("Category");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 130, -1, -1));

        jLabel3.setText("Supplier");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 160, -1, -1));

        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });
        jPanel1.add(btnsave, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 300, 70, -1));

        jLabel5.setText("Enter new Stock details.");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        btncls.setText("Close");
        btncls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnclsActionPerformed(evt);
            }
        });
        jPanel1.add(btncls, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 300, 70, -1));

        jLabel6.setText("Brand Name");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 100, -1, -1));

        qty.setBackground(new java.awt.Color(219, 237, 255));
        qty.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                qtyKeyPressed(evt);
            }
        });
        jPanel1.add(qty, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 200, 90, -1));

        jLabel7.setText("Quantity");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 200, -1, -1));

        expDate.setBackground(new java.awt.Color(219, 237, 255));
        expDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expDateActionPerformed(evt);
            }
        });
        expDate.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                expDateKeyPressed(evt);
            }
        });
        jPanel1.add(expDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 250, 150, -1));

        jLabel8.setText("Expire Date");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 250, -1, -1));

        supplier.setText("--");
        jPanel1.add(supplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 160, -1, -1));

        brandName.setText("--");
        jPanel1.add(brandName, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 100, -1, -1));

        category.setText("--");
        jPanel1.add(category, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 130, -1, -1));

        searchItem.setText("Select Medicine");
        searchItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchItemActionPerformed(evt);
            }
        });
        jPanel1.add(searchItem, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 20, 140, -1));

        jLabel9.setText("Code");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 70, -1, -1));

        code.setText("--");
        jPanel1.add(code, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 70, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnclsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnclsActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnclsActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        if (code.getText().equals("--")) {
            JOptionPane.showMessageDialog(null, "Select a Medicine !", "Save Stock", 2);
        } else if (qty.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Quantity !", "Save Stock", 2);
        } else if (expDate.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Expire Date !", "Save Stock", 2);
        } else {
            Stock stock = new Stock();
//            stock.setItem(selectedItem);
//            stock.setQty(Integer.parseInt(qty.getText()));
//            stock.setExpiryDate(expDate.getText());

            boolean save = stockService.save(stock);

            if (save) {
                code.setText("--");
                brandName.setText("--");
                category.setText("--");
                supplier.setText("--");
                qty.setText(null);
                expDate.setText(null);
                unit.setText("Unit");
            }
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void qtyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_qtyKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_qtyKeyPressed

    private void expDateKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_expDateKeyPressed
        DatePicker datePicker = new DatePicker(jPanel1);
        expDate.setText(datePicker.setPickedDate());
    }//GEN-LAST:event_expDateKeyPressed

    private void expDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expDateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_expDateActionPerformed

    private void searchItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchItemActionPerformed
        searchItemForm.setVisible(true);
        searchItemForm.setSelectedItemNull();
        code.setText("--");
        brandName.setText("--");
        category.setText("--");
        supplier.setText("--");
        unit.setText("Unit");
        new Thread(new Runnable() {

            public void run() {
                while (searchItemForm.getSelectedItem() == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Logger.getLogger(NewStock.class.getName()).error("Thread failed");
                    }
                }
                selectedItem = searchItemForm.getSelectedItem();
                if (selectedItem.getId() != 0) {
                    code.setText(selectedItem.getItemCode());
                    brandName.setText(selectedItem.getBrandName());
                    category.setText(selectedItem.getCategory());
                    supplier.setText(selectedItem.getSupplier());
                    unit.setText(selectedItem.getUnit());
                }
            }
        }).start();
    }//GEN-LAST:event_searchItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewStock().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel brandName;
    private javax.swing.JButton btncls;
    private javax.swing.JButton btnsave;
    private javax.swing.JLabel category;
    private javax.swing.JLabel code;
    private javax.swing.JTextField expDate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JTextField qty;
    private javax.swing.JButton searchItem;
    private javax.swing.JLabel supplier;
    private javax.swing.JLabel unit;
    // End of variables declaration//GEN-END:variables
}
